﻿using System;
using System.Runtime.CompilerServices;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;

namespace Nirvana
{
    [CustomEditor(typeof(AnimatorResetBehaviour))]
    public class AnimatorResetBehaviourEditor : Editor
    {
        private SerializedProperty enterResets;
        private SerializedProperty exitResets;
        private ReorderableList enterList;
        private ReorderableList exitList;


        public override void OnInspectorGUI()
        {
            base.serializedObject.Update();
            GUILayout.Space(EditorGUIUtility.singleLineHeight);
            this.enterList.DoLayoutList();
            GUILayout.Space(EditorGUIUtility.singleLineHeight);
            this.exitList.DoLayoutList();
            base.serializedObject.ApplyModifiedProperties();
        }

        private void OnEnable()
        {
            if (this.target == null) return;
            this.enterResets = serializedObject.FindProperty("enterResets");
            this.exitResets = serializedObject.FindProperty("exitResets");

            this.enterList = new ReorderableList(serializedObject, this.enterResets);
            this.enterList.drawHeaderCallback = DrawHeaderEnter;
            this.enterList.elementHeight = 1.3f * EditorGUIUtility.singleLineHeight;
            this.enterList.drawElementCallback = DrawElementEnter;

            this.exitList = new ReorderableList(serializedObject, this.exitResets);
            this.exitList.drawHeaderCallback = DrawHeaderExit;
            this.exitList.elementHeight = 1.3f * EditorGUIUtility.singleLineHeight;
            this.exitList.drawElementCallback = DrawElementExit;
        }

        private void DrawHeaderEnter(Rect rect)
        {
            GUI.Label(rect, "Enter Parmas:");
        }
        private void DrawHeaderExit(Rect rect)
        {
            GUI.Label(rect, "Exit Parmas:");
        }

        private void DrawElementEnter(Rect rect, int index, bool isActive, bool isFocused)
        {
            DrawElement(this.enterResets, rect, index, isActive, isFocused);
        }
        private void DrawElementExit(Rect rect, int index, bool isActive, bool isFocused)
        {
            DrawElement(this.exitResets, rect, index, isActive, isFocused);
        }
        private void DrawElement(SerializedProperty prop, Rect rect, int index, bool isActive, bool isFocused)
        {
            SerializedProperty arrayElementAtIndex = prop.GetArrayElementAtIndex(index);
            Rect r = new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight);
            EditorGUI.PropertyField(r, arrayElementAtIndex, GUIContent.none);
        }





    }
}

